package cn.donting.jme.utile;

import cn.hutool.setting.Setting;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class SettingUtil {

    public static void write(Object object, File file) throws Exception {
        ObjectMapper objectMapper=new ObjectMapper();
        String s = objectMapper.writeValueAsString(object);
        HashMap<String,Object> hashMap = objectMapper.readValue(s, HashMap.class);
        Setting setting=new Setting(file.toURI().toURL().getPath());
        for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            setting.set(key,value.toString());
        }
        setting.store();
    }

    public static <T> T read(Object o,File file) throws Exception {

        Setting setting = new Setting(file.getPath());
        Object bean = setting.toBean(o);
        return (T)bean;
    }
}
